package be.ipl.eBar.domaine;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

import be.ipl.eBar.exceptions.ArgumentInvalideException;
import be.ipl.eBar.util.Util;

@SuppressWarnings("serial")
@Entity
@Table(name = "BIERES", schema = "EBAR")
public class Biere implements Serializable {
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private int id;

	@NotNull
	@Column(unique = true)
	private String nom;

	@NotNull
	private String type;

	@NotNull
	private String couleur;

	@NotNull
	@ManyToOne
	@JoinColumn(name = "BRASSERIE_ID")
	private Brasserie brasserie;

	@ManyToMany(mappedBy="bieres")
	private List<Fournisseur> fournisseurs = new ArrayList<Fournisseur>();

	@NotNull
	@Min(0)
	@Max(15)
	private double prix;

	protected Biere() {
	}

	public Biere(String nom, String type, String couleur, Brasserie brasserie,
			double prix) throws ArgumentInvalideException {
		Util.checkString(nom);
		Util.checkString(type);
		Util.checkString(couleur);
		Util.checkObject(brasserie);
		Util.checkPositive(prix);
		this.nom = nom;
		this.type = type;
		this.couleur = couleur;
		this.brasserie = brasserie;
		this.prix = prix;
	}

	public int getId() {
		return id;
	}

	public String getNom() {
		return nom;
	}

	public String getType() {
		return type;
	}

	public String getCouleur() {
		return couleur;
	}

	public Brasserie getBrasserie() {
		return brasserie;
	}

	public double getPrix() {
		return prix;
	}
	
	public List<Fournisseur> getFournisseurs() {
		return fournisseurs;
	}

	public void setPrix(double prix) throws ArgumentInvalideException {
		Util.checkPositive(prix);
		this.prix = prix;
	}

	@Override
	public boolean equals(Object o) {
		if (o == null)
			return false;
		if (!(o instanceof Biere))
			return false;
		Biere e = (Biere) o;
		if (e.getClass() != this.getClass())
			return false;
		return this.getId() == e.getId();
	}

	@Override
	public int hashCode() {
		return getId();
	}


}
